/**
 * samplelib.js
 * v0.01
 * 3/7/11
 * 
 * The following are Javascript functions for adding samples.
 */

/**
 * The function below can add new samples.
 */
function newSample(){	
	form = document.getElementById("main-form");
	
	countervalue = parseInt(document.getElementById("counter").value);
	imdone = document.getElementById("imdone");
	addsample = document.getElementById("addsample");
	
	table = document.getElementById(countervalue);
	
	newtable = table.cloneNode(true);
	document.getElementById("counter").value = parseInt(document.getElementById("counter").value) + 1;
	newtable.id = countervalue + 1;
	newtable.getElementsByTagName("input")[6].id = (100+ 1 + countervalue);
	
	form.appendChild(newtable);
	form.appendChild(imdone);
	form.appendChild(addsample);
	
	var date = document.getElementsByName('samplingdate');
	var sign = document.getElementsByName('samplesignature');
	var others = document.getElementsByName('othersField');
	var req = document.getElementsByName('analysisreq');
	var sub = document.getElementsByName('datesubmit');
	var rec = document.getElementsByName('samplereceive');
	var remark = document.getElementsByName('remarks');
	
	var samplingDateError = document.getElementsByName('samdate');
	var sampleSubError = document.getElementsByName('sample');
	var dateSubError = document.getElementsByName('datesub');
	var sampleRecError = document.getElementsByName('samreceived');
	var here = document.getElementsByName('here');

	date[newtable.id-1].value = "";
	sign[newtable.id-1].value = "";
	others[newtable.id-1].value = "";
	req[newtable.id-1].value = "";
	sub[newtable.id-1].value = "";
	rec[newtable.id-1].value = "";
	remark[newtable.id-1].value = "";	
	
	samplingDateError[newtable.id-1].innerHTML = "";
	sampleSubError[newtable.id-1].innerHTML = "";
	dateSubError[newtable.id-1].innerHTML = "";
	sampleRecError[newtable.id-1].innerHTML = ""
	here[newtable.id-1].innerHTML = "";
}


/**
 * The follwoing function deletes samples.
 */
function deleteSample(){
	var e = event.srcElement.id ;
	countervalue = parseInt(document.getElementById("counter").value);
	form = document.getElementById("main-form");
	table = document.getElementById(parseInt(e) - 100);
	form.removeChild(table);
	
	//Clean up the rest of the tables
	j = (parseInt(e)-100);
	for( i = parseInt(e)-99; i <= countervalue; i++){
		document.getElementById(i).id = j++;
		document.getElementById(i+100).id = 99 + parseInt(j);
	} 
	document.getElementById("counter").value = parseInt(document.getElementById("counter").value)- 1;
}

/**
 * This function does not allow any symbol to be written on the text box.
 */
function onlyAlphaNum(){
	var e = event || evt;
    var charCode = e.which || e.keyCode;
    if ((charCode > 31 && charCode < 48) || (charCode > 57 && charCode < 65))
        return false;
    return true;
}

/**
 * This function checks if the date format (mm-dd-yyyy) is followed.
 */
function checkDateFormat(){
	var validDate = /^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$/);
	var samDate = validDate.test(addSample.samplingdate.value)
	var dateSub = validDate.test(addSample.datesubmit.value);
	var samDateError = document.getElementById('samdate');
	var dateSubError = document.getElementById('datesub');
	
	var sampleSubmittedError = document.getElementById('sample');
	var sampleReceivedError = document.getElementById('samreceive');
	if (samDate == false || dateSub == false) {
		if (samDate == false){
			samDateError.innerHTML = "Not following date format mm-dd-yyyy";
		} 
		if (dateSub == false){
			dateSubError.innerHTML = "Not following date format mm-dd-yyyy";
		} 
		return false;
	} else {
		return true;
	}
}

/**
 * The function enables and disables a text box.
 */
function change(){
	var currentEnabled = new Array();
	currentEnabled[0] = document.addSample.othersField;
	var index = document.addSample.mainTypes.selectedIndex;
	var selectedOption = document.addSample.mainTypes.options[index].text;
	if(selectedOption == "Others..."){
		currentEnabled[0].disabled = false;
	} else {
		currentEnabled[0].disabled = true;
	}
}

/**
 * The function checks if text fields below are empty or not.
 */
function checkEmpty(){
	var samplingDateError = document.getElementById('samdate');
	var sampleSubError = document.getElementById('sample');
	var dateSubError = document.getElementById('datesub');
	var sampleRecError = document.getElementById('samreceived');
	var retVal = false;
	if ((addSample.samplingdate.value == "") 
		|| (addSample.samplesignature.value == "") 
		|| (addSample.datesubmit.value == "") 
		|| (addSample.samplereceive.value == "")) {
		if (addSample.samplingdate.value == "") {
			samplingDateError.innerHTML = "Empty Sampling Date";
		} else {
			retVal = checkDateFormat();
			return retVal;
		}
		if (addSample.samplesignature.value == "") {
			sampleSubError.innerHTML = "Empty Sample Submitted by";
		}
		if (addSample.datesubmit.value == "") {
			dateSubError.innerHTML = "Empty Date Submitted";
		} else {
			retVal = checkDateFormat();
			return retVal;
		}
		if (addSample.samplereceive.value == "") {
			sampleRecError.innerHTML = "Empty Sample Received by";
		}
		return false;
	} else {
		retVal = checkDateFormat();
		return retVal;
	}
}